package com.itheima.controller;

import com.itheima.pojo.Address;
import com.itheima.pojo.User;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author Xiao
 * @date 2021/9/14
 */
@Controller //定义表现层控制器bean
//类上方配置的请求映射与方法上面配置的请求映射连接在一起，形成完整的请求映射路径
@RequestMapping("/user")
public class UserController {
    //设置映射路径为/save，即外部访问路径
    @RequestMapping("/save")    //此时save方法的访问路径是：/user/save
    //设置当前操作返回结果为指定json字符串
    @ResponseBody
    public String save() {
        System.out.println("UserController save...");
        return "{'info':'springMVC UserController save...'}";
    }

    @RequestMapping("/delete")
    @ResponseBody
    public String delete() {
        System.out.println("UserController delete...");
        return "{'info':'springMVC UserController delete...'}";
    }

    @RequestMapping("/commonParam")
    @ResponseBody
    //普通参数 --> 直接在方法形参上接收
    public String commonParam(String name, int age) {
        System.out.println("传递普通参数 name --> " + name);
        System.out.println("传递普通参数 age --> " + age);
        return "{'info':'springMVC UserController commonParam...'}";
    }

    @RequestMapping("/commonParamDifferName")
    @ResponseBody
    //@RequestParam --> 绑定请求参数与处理器方法形参间的关系
    public String commonParamDifferName(@RequestParam("name") String username, int age) {
        System.out.println("传递普通参数 username --> " + username);
        System.out.println("传递普通参数 age --> " + age);
        return "{'info':'springMVC UserController commonParamDifferName...'}";
    }

    @RequestMapping("/pojoParam")
    @ResponseBody
    //如果传入的参数key名称和实体类的属性名一致，可以自动封装
    public String pojoParam(User user) {
        System.out.println("pojo参数接收普通参数 user --> " + user);
        return "{'info':'springMVC UserController pojoParam...'}";
    }

    @RequestMapping("/pojoContainPojoParam")
    @ResponseBody
    //嵌套pojo：
    //User --> String name, Integer age, Address address
    //Address --> String province, String city
    //传参方式：name="张三"  age=23    address.province="湖南"   address.city="长沙"
    public String pojoContainPojoParam(User user) {
        System.out.println("pojo参嵌套pojo参数 user --> " + user);
        return "{'info':'springMVC UserController pojoContainPojoParam...'}";
    }

    @RequestMapping("/arrayParam")
    @ResponseBody
    //数组传参：传过来的参数键名称都相同
    public String arrayParam(String[] likes) {
        System.out.println("数组传参 likes --> " + Arrays.toString(likes));
        return "{'info':'springMVC UserController arrayParam...'}";
    }

    @RequestMapping("/listParam")
    @ResponseBody
    //集合传参：传过来的参数键名称都相同，
    // 接收时形参上使用@RequestParam注解
    public String listParam(@RequestParam List<String> likes) {
        System.out.println("集合传参 likes --> " + likes);
        return "{'info':'springMVC UserController listParam...'}";
    }

    @RequestMapping("/listParamForJson")
    @ResponseBody
    //集合传JSON数据：传过来的参数键名称都相同，
    // 接收时形参上使用@RequestBody注解
    public String listParamForJson(@RequestBody List<String> likes) {
        System.out.println("集合传JSON数据 likes --> " + likes);
        return "{'info':'springMVC UserController listParamForJson...'}";
    }

    @RequestMapping("/pojoContainPojoParamForJson")
    @ResponseBody
    //嵌套pojo：
    //User --> String name, Integer age, Address address
    //Address --> String province, String city
    //传参方式：{"name":"张三",
    //          "age":23,
    //          "address":{"province":"湖南","city":"长沙"}}
    public String pojoContainPojoParamForJson(@RequestBody User user) {
        System.out.println("Json传递pojo参嵌套pojo参数 user --> " + user);
        return "{'info':'springMVC UserController pojoContainPojoParamForJson...'}";
    }

    @RequestMapping("/listPojoParamForJson")
    @ResponseBody
    //嵌套pojo：
    //User --> String name, Integer age, Address address
    //Address --> String province, String city
    //传参方式：{"name":"张三","age":23},{"name":"张三","age":23}
    public String listPojoParamForJson(@RequestBody List<User> list) {
        System.out.println("Json传递多个pojo参数 user --> " + list);
        return "{'info':'springMVC UserController listPojoParamForJson...'}";
    }

    @RequestMapping("/dateParam")
    @ResponseBody
    //传递日期类型参数,默认格式yyyy/MM/dd
    //如果想要传递和接收其他格式，需要使用@DateTimeFormat注解并指定pattern属性
    public String dateParam(Date date1,
                            @DateTimeFormat(pattern = "yyyy-MM-dd") Date date2,
                            @DateTimeFormat(pattern = "yyyy/MM/dd HH:mm:ss") Date date3) {
        System.out.println("日期参数 date1 --> " + date1);
        System.out.println("日期参数 date2 --> " + date2);
        System.out.println("日期参数 date3 --> " + date3);
        return "{'info':'springMVC UserController dataParam...'}";
    }

    @RequestMapping("/toJumpPage")
    //响应页面/跳转页面
    //返回值为String类型，设置返回值为页面名称(注意路径)
    public String toJumpPage() {
        System.out.println("跳转页面");
        return "../hello.jsp";
    }

    //响应文本数据
    @RequestMapping("/toText")
    @ResponseBody
    public String toText() {
        System.out.println("响应纯文本数据。。。");
        return "Text...";
    }

    //响应JSON格式的pojo
    @RequestMapping("/toJsonPojo")
    @ResponseBody
    public User toJsonPojo() {
        System.out.println("响应json对象数据。。。");
        User user = new User();
        user.setName("张三");
        user.setAge(23);
        Address address = new Address();
        address.setProvince("湖南");
        address.setCity("长沙");
        user.setAddress(address);
        return user;
    }

    //响应JSON对象集合
    @RequestMapping("/toJsonPojoList")
    @ResponseBody
    public List<User> toJsonPojoList() {
        System.out.println("响应json对象集合数据。。。");
        User user1 = new User();
        user1.setName("张三");
        user1.setAge(23);
        User user2 = new User();
        user2.setName("李四");
        user2.setAge(24);
        List<User> list = new ArrayList<>(List.of(user1, user2));
        return list;
    }
}
